<!-- 头盔弹出框 -->
<template>
  <div class="helmet">
    <div v-if="initFlag" class="left">
      <div id="msg">
        <div class="msg-box" v-for="(item, i) in pulist" :key="i">
          <ul :class="item._info_pu.onlinestatus == 1 ? 'online' : ''">
            <li>
              <span>ID: </span>
              <span>{{ item._info_pu.id }}</span>
            </li>
            <li>
              <span>型号: </span>
              <span>{{ item._info_pu.name }}</span>
            </li>
            <li>
              <span>状态: </span>
              <span>{{
                item._info_pu.onlinestatus == 1 ? "在线" : "离线"
              }}</span>
            </li>
            <li>
              <span>开机时间: </span>
              <span>{{ item._info_pu.bootduration }}</span>
            </li>
            <li class="tool">
              <el-button
                v-if="item._info_pu.onlinestatus == 1"
                type="success"
                @click="helmetVideo(item._info_pu.id)"
                >现场视频</el-button
              >
              <el-button v-else type="success" disabled>无视频</el-button>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div v-if="initFlag" class="main">
      <div
        class="video-box"
        v-if="videoFlag"
        v-loading="!videoFlag"
        element-loading-text="视频加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <div id="id_video" style="width: 100%; height: 100%"></div>
      </div>
      <div class="hdlg">通道句柄：{{ hdlgNum }}</div>
    </div>
    <div v-if="!initFlag" v-loading="true" class="init-loading"></div>
  </div>
</template>

<script>
import Jsw from "../../assets/js/Jsw";

var jSW;
var session = null;

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},

  //父组件传入子组件数据
  props: {},

  //格式化数据
  filters: {},

  data() {
    //这里存放数据
    return {
      title: "",
      initFlag: false,
      pulist: null,
      videoFlag: true,
      hdlgNum: 0,
    };
  },

  //计算属性 类似于data概念
  computed: {},

  //监控data中的数据变化
  watch: {},

  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},

  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.init();
  },

  //方法集合
  methods: {
    init() {
      this.initFlag = false;
      var that = this;
      Jsw.then(() => {
        jSW = window.jSW;
        // console.log(jSW);

        jSW.swInit({
          url: "http://153.99.98.91:8081",
          calltype: jSW.CallProtoType.AUTO,
          oninit: that.testLogin,
        });
      });
    },

    // 登录
    testLogin() {
      if (session) {
        session = null;
      }
      session = new jSW.SWSession({
        server: "153.99.98.91",
        port: 9701,
        onopen: function (sess) {
          sess.swLogin({
            user: "admin",
            password: "123456",
          });
        },
      });

      // 注册事件的回调函数
      session.swAddCallBack("login", this.sessionCallback);
      session.swAddCallBack("logout", this.sessionCallback);
      session.swAddCallBack("pulist", this.sessionCallback);
    },

    // 注册的回调
    sessionCallback(sender, event, json) {
      var info = "";
      if ("login" == event) {
        info += "登录";
      } else if ("logout" == event) {
        info += "退出";
      } else if ("pulist" == event) {
        info += "获得设备列表";
      }

      if (json.code == jSW.RcCode.RC_CODE_S_OK) {
        info += "成功";
      } else {
        info += "失败, error code: " + json.code;
      }

      //   document.getElementById("msg").innerHTML = info;
      //   this.testShowPuList()
      this.pulist = session._arr_pu;
      console.log(this.pulist);

      this.initFlag = true;
    },

    // 播放视频
    helmetVideo(id) {
      console.log("pu_id: ", id);
      var chanel = session.swGetPuChanel(id, 0);

      if (this.hdlg != 0) {
        console.log("关闭上一个通道：" + this.hdlgNum);
        chanel.swClose(this.hdlgNum);
      }
      this.videoFlag = true;
      
      var _this = this;

      if (chanel) {
        var result = chanel.swOpenEx({
          ismuti: true,
          div: "id_video",
          prototype: "auto", //rtmp > hls
          callback: async function (options, response, dlghandle) {
            // console.log(response.emms.code + "视频打开成功，可以获取URl了");
            // alert(response.emms.code + "视频打开成功，可以获取URl了");
            _this.hdlgNum = dlghandle.hdlg;
            console.log("句柄：" + _this.hdlgNum);
            // chanel.swClose(this.hdlg)
            var url = chanel.swGetUrl();
            console.log("url ------------------------------");
            console.log(url);
          },
          media: 10,
        });
        var url = chanel.swGetUrl();
        console.log("url 11 ------------------------------");
        console.log(url);

        if (result != jSW.RcCode.RC_CODE_S_OK) {
          console.log("打开视频失败: " + result);
        }
      } else {
        console.log("没有该设备通道");
      }
    },
  },

  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>

<style scoped lang='less'>
/* 使用$符号可以标识一个变量;@import url(); 引入公共css类 */
.helmet {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  border: 1px solid #ccc;
  display: flex;
  .left {
    width: 20%;
    height: 100%;
    // background-color: #f5f5f5;
    #msg {
      // background-color: #f5f5f5;
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      .msg-box {
        width: 100%;
        flex: 1;
        box-sizing: border-box;
        padding: 0.1rem;
      }
      ul {
        width: 100%;
        height: 100%;
        box-sizing: border-box;
        padding: 0.2rem;
        display: flex;
        flex-direction: column;
        // border: 2px solid @Blue;
        background-color: #111a6e;
        border-radius: 0.1rem;
        opacity: 0.7;
        li {
          width: 100%;
          flex: 1;
          display: flex;
          span {
            display: block;
            width: 50%;
            height: 100%;
            color: #fff;
            font-weight: 700;
            font-size: 0.18rem;
          }
        }
        li.tool {
          display: flex;
          justify-content: flex-end;
        }
      }
      ul.online {
        opacity: 1;
      }
    }
  }
  .main {
    width: 80%;
    height: 100%;
    box-sizing: border-box;
    padding: 0.1rem;
    position: relative;
    .video-box {
      width: 100%;
      height: 100%;
      background-color: #000;
      #id_video {
        background-color: #000;
      }
    }
    .hdlg {
      width: 3rem;
      height: 1rem;
      line-height: 1rem;
      background-color: #00f;
      position: absolute;
      color: #fff;
      font-size: 0.24rem;
      font-weight: 700;
      top: 2rem;
      left: 2rem;
    }
  }

  .init-loading {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>